/**
 * 
 */
package com.bodystm.temperature.web;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.bodystm.temperature.patient.PatientBedDao;
import com.bodystm.temperature.patient.PatientBedService;
import com.bodystm.temperature.patient.PatientBedUser;
import com.bodystm.temperature.server.Temperature;
import com.bodystm.temperature.server.TemperatureService;
import com.bodystm.temperature.server.UrineBag;
import com.bodystm.temperature.server.UrineBagService;

/**
 * @author rxz
 *
 */
public class DeviceStatusCheck implements Runnable {
	@Autowired
	private PatientBedService patientBedUserService;

	@Autowired
	private PatientBedDao patientBedUserDao;
	
	@Autowired
	private UrineBagService urineBagService;
	
	@Autowired
	private Integer checkminutes = 10;
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		//��ȡ��ǰ�������в��˵Ĵ�λ����Ϣ
		List<PatientBedUser> lstpatientBedUser=patientBedUserDao.getCurrentPatients();
		Calendar calendar = Calendar.getInstance();
		Date date=new Date();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, -checkminutes);
		date = calendar.getTime();
		UrineBag urineBag=null;
		for(PatientBedUser patientBedUser:lstpatientBedUser){
			//temperatureService.query(patientBedUser.getPatient().getEquipNo(),new Date(),new date(),null);
			urineBag=urineBagService.getLatestPoint(date,patientBedUser.getPatient().getEquipNo());
			if(patientBedUser.getStatusOfDevice()==null){
				patientBedUser.setStatusOfDevice(1);
			}
			//����ȡ����������ʾ���ֹ����ˣ���Ҫ�޸�patientBedUser���豸״̬
			if(urineBag==null){
				if(patientBedUser.getStatusOfDevice()==1){
					patientBedUser.setStatusOfDevice(0);
					patientBedUserService.saveEntity(patientBedUser);
				}
			}else{
				if(patientBedUser.getStatusOfDevice()==0){
					//将该设备的失去信号提醒的标志位重置为true
					if(DeviceSettings.lstMacs.contains(patientBedUser.getPatient().getEquipNo())){
						UrineBagDevice urineBagDevice=(UrineBagDevice)DeviceSettings.hasDevice.get(patientBedUser.getPatient().getEquipNo());
						urineBagDevice.setWarningOrNot4NoPower(true);
					}
					patientBedUser.setStatusOfDevice(1);
					patientBedUserService.saveEntity(patientBedUser);
				}
			}
		}

	}

}
